/**
 * 
 */
package com.vision.core.sv.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Mark
 *
 */
public class BeanValidatorFactory {
	
	private static Map<Class<?>, IBeanValidator<?>> validatorCache;
	
	@SuppressWarnings("unchecked")
	public static <T> List<String> validate(T object) {
		IBeanValidator<T> v = (IBeanValidator<T>) getValidator(object.getClass());
		return v.validate(object);
	}
	
	@SuppressWarnings("unchecked")
	public static <T> IBeanValidator<T> getValidator(Class<?> clazz) {
		if (validatorCache == null)
			validatorCache = new HashMap<Class<?>, IBeanValidator<?>>();
		
		if (validatorCache.containsKey(clazz))
			return (IBeanValidator<T>) validatorCache.get(clazz);
		
		IBeanValidator<T> v = new BeanValidator<T>((Class<T>) clazz);
		validatorCache.put(clazz, v);
		
		return v;
	}

}
